package com.gopay.solution.domain.bo.impl;

import com.gopay.common.constants.trans.TranSpCustType;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.solution.TranBasicReq;
import com.gopay.common.domain.solution.TranBasicRes;
import com.gopay.solution.domain.po.SolutionRuleDtl;
import com.gopay.solution.domain.po.SsTransBasicDtl;
import com.gopay.solution.domain.vo.Event;
import com.gopay.solution.domain.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 方案服务 交易基础方案配置明细 规则执行器
 * 
 * @SsTransBasicSoluRuleExecutor.java
 * @author MA HUAN
 * @2013-3-27 下午4:28:51 www.gopay.com.cn Inc.All rights reserved.
 */
@Service(value = "ssTransBasicSoluRuleExecutor")
public class SsTransBasicSoluRuleExecutor extends AbstractSolutionRuleExecutor {

    @Override
    public Result execute(List<SolutionRuleDtl> rules, Event event) {
       

        Result result = new Result();
        result.setSolved(false);
        result.setFailedReason("");
        result.setDetail(null);

        if (null == rules || rules.isEmpty()) {
            result.setFailedReason("查询不到基础方案记录");
            return result;
        }

        Serializable input = event.getDetail();

        TranBasicReq req = (TranBasicReq) input;
        IntTxnCd intTxnCd = req.getIntTxnCd();

        SsTransBasicDtl tranBasicDtl = null;
        for (SolutionRuleDtl rule : rules) {
            
            SsTransBasicDtl tmp = (SsTransBasicDtl)rule;
            if(tmp.getGopayIntTxnCd().equals(intTxnCd.value)) {
                tranBasicDtl = tmp;
                break;
            }
        }
        
        if(tranBasicDtl != null) {
            TranBasicRes res = new TranBasicRes();
            res.setEnable(tranBasicDtl.getIsEnable() == 1);
            res.setFeePayer(tranBasicDtl.getFeePayer());
            res.setSettleCircle(tranBasicDtl.getSettleCirCle());
            res.setRefund(tranBasicDtl.getIsRefundFee() == 1);
            res.setLegalIncomeAcct(tranBasicDtl.getLegalIncomeAcct());
            //默认都支持
            res.setTranSpCustType(
            		StringUtils.isBlank(tranBasicDtl.getTranSpCustType()) ? 
            		TranSpCustType.ALL : TranSpCustType.getTranSpCustTypeByValue(tranBasicDtl.getTranSpCustType()));
            res.setRefundPeriod(tranBasicDtl.getRefundPeriod());
            res.setIsMobileNotify(tranBasicDtl.getIsMobileNotify()==null?-1:tranBasicDtl.getIsMobileNotify()); //短信通知
            res.setIsOverTimeRefund(null == tranBasicDtl.getIsOverTimeRefund() ? -1:tranBasicDtl.getIsOverTimeRefund());
            res.setEndRefundPeriod(null == tranBasicDtl.getEndRefundPeriod() ? -1 : tranBasicDtl.getEndRefundPeriod());
            //add by qld start  跨境人民币进口分账比例
            res.setCbrmbImportSplit(null==tranBasicDtl.getCbrmbImportSplit()?new BigDecimal("0"):tranBasicDtl.getCbrmbImportSplit());
            //end by qld
            //add by qld start  外卡保证金 20171107
            res.setWkDepositScale(null==tranBasicDtl.getWkDepositScale()?new BigDecimal("0"):tranBasicDtl.getWkDepositScale());
           //end by qld
            result.setSolved(true);
            result.setFailedReason("");
            result.setDetail(res);
        }

        
        
        return result;
    }

}
